<?php

namespace App\Repositories;

use App\Exceptions\BasicException;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use function Matrix\add;    //引入hash

class VirusChancePrizeRepository {

    /*
     * 获取概率奖池列表
     * @param $type
     * @return bool
     * @throws BasicException
     * */
    public function getChancePrizeList($type=3) {
        $list = app('model_virus_chance_prize')
            ->select('id','initial_weight','type','percent','gift_id')
            ->where('type',$type)
            ->with('gift:id,name,price,picture,special')
            ->get()->toArray();
        return $list;
    }

     /*
     * 某一类型奖池初始权重的总和
     * @param $type
     * @return bool
     * @throws BasicException
     * */

     public  function  totalInitialWeight($type){
         $totalInitialWeight = app('model_virus_chance_prize')->where('type',$type)->sum('initial_weight');
         return  $totalInitialWeight ?? 0;
     }


    /*
    *验证礼物是否存在
    * @param $type
    * @param $gift_id
    * @param $percent
    * @return bool
    * @throws BasicException
    * */
    public  function  verifyGift($type,$gift_id,$percent){
        $add = app('model_virus_chance_prize')->where(['type'=>$type,'gift_id'=>$gift_id,'percent'=>$percent])->first();
        if ($add) throw new BasicException(50020);
    }


    /*
     *新增概率奖池礼物
     * @param $param
     * @return bool
     * @throws BasicException
     * */
    public  function  addGift($param){
        $add = app('model_virus_chance_prize')->create($param);
        if ($add) return $add;
        throw new BasicException(93093);
    }

        /*
     *编辑概率奖池礼物
     * @param $param
     * @return bool
     * @throws BasicException
     * */
    public function  saveGift($param){
        $save = app('model_virus_chance_prize')->where('id',$param['id'])->update($param);
        if ($save) return $save;
        throw new BasicException(93094);
    }

     /*
    *删除概率奖池礼物
    * @param $arr_ids
    * @return bool
    * @throws BasicException
    * */
    public function delChancePrize($arr_ids){
        $del = app('model_virus_chance_prize')->whereIn('id',$arr_ids)->delete();
        if ($del)  return $del;
        throw new BasicException(93100);
    }

}

?>